import { ATeamCombat } from "../TeamCombat";

export class DismantleStrategy
{
    private static attackAroundStructures(creep:Creep)
    {
        const cachestructure=  creep.room.getWorld().PollStructure(creep.pos.roomName);
        if(cachestructure)
        {
            const structures = cachestructure.findInRange(creep.pos,1).filter(e=>e.structureType!== STRUCTURE_ROAD  && e.structureType!== STRUCTURE_CONTAINER);;

            if(structures.length==0)
            {
                return false;
            }
            for(const e of structures)
            {
                const distance = creep.pos.distance(e.pos);
                if(distance<=1 && !e.room.IsMy())
                {
                    creep.dismantle(e);
                    return true;
                }
            }
        }
        return false;
    }
    
    private static attackTargets(creep:Creep,targets:Id<Structure>[])
    {
        for(const e of targets)
        {
            const obj = Game.getObjectById(e);
            if(obj && creep.pos.distance(obj.pos)<=1)
            {
                creep.dismantle(obj);
                return true
            }
        }
        return false;
    }
    /**
     * 从target中选取歼灭目标，和骚扰目标进行攻击
    */
    
    public static attack(team:ATeamCombat,targets:string[])
    {
        const attackers = team.getAttacker();
        for(const e of attackers)
        {
            if(!this.attackTargets(e,targets as Id<Structure>[]))
            {
                this.attackAroundStructures(e);
            }
        }
    }
}